package com.lzp.customview

import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.view.loadmore.LoadMoreAdapter

/**
 * @describe:
 * @Author: lixiaopeng
 * @Date: 2020/7/10
 */
class CommentLoadMoreAdapter : LoadMoreAdapter {

    private val TAG = "CommentLoadMoreAdapter"
    private val list = arrayListOf<String>()

    constructor(size: Int) : super() {
        for (i in 0 until size) {
            list.add(i.toString())
        }
    }


    fun loadMore() {
        val mGetItemCount = mGetItemCount()
        for (i in 1..10) {
            list.add((i + mGetItemCount).toString())
        }
        notifyItemRangeChanged(mGetItemCount-1,mGetItemCount(),"666")
    }

    fun doTest(){
        notifyItemRangeChanged(0,mGetItemCount(),"666")
    }

    override fun mOnCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        val holder = MyHolder(LayoutInflater.from(parent.context).inflate(R.layout.test_rv_item, parent, false))
        holder.itemView.setOnClickListener {
            doTest()
        }
        return holder
    }

    override fun mGetItemCount(): Int = list.size

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int, payloads: MutableList<Any?>) {
        Log.e(TAG, "mOnBindViewHolder: $position  ${payloads.isEmpty()}")
        super.onBindViewHolder(holder, position, payloads)
    }

    override fun mOnBindViewHolder(viewHolder: RecyclerView.ViewHolder, position: Int) {
        if (viewHolder !is MyHolder) {
            return
        }
        val s = list[position]
        viewHolder.tv.text = s
    }

    private class MyHolder : RecyclerView.ViewHolder {
        val tv: TextView

        constructor(itemView: View) : super(itemView) {
            tv = itemView.findViewById(R.id.tv)
        }
    }

}